package com.tarena.micro.service.gateway.filters;

import com.alibaba.nacos.common.utils.CollectionUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;
import java.util.Collection;
import java.util.List;

//成为容器bean对象
//@Component
public class My02Filter implements GlobalFilter , Ordered {
    /**
     * @param exchange 交换机 包装了当前过滤器处理的请求和响应对象
     * @param chain 表示连接过滤链的对象
     * @return 是spring webflux框架的一个通用结构的返回对象,作为响应数据返回,和springmvc中的model类似
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("进入我的第二个过滤器");
        //1.拿到进入这个过滤器的一个请求对象
        ServerHttpRequest request = exchange.getRequest();
        //2.读取path路径,看看进入到这个过滤器的请求path是什么 http://localhost:10000/cart/hello?name=wang
        URI uri = request.getURI();
        System.out.println("获取到请求uri:"+uri);
        String path = uri.getPath();
        System.out.println("获取请求的path:"+path);
        String host = uri.getHost();
        System.out.println("获取请求的host:"+host);
        String query = uri.getQuery();
        System.out.println("获取请求的query:"+query);
        //3.query的数据,在网关过滤器中有专门的解析对象map
        MultiValueMap<String, String> queryParams = request.getQueryParams();
        //map第一个泛型是query的key值 第二个泛型是query的value值 但是会封装到list中
        //name=王翠花 key=name vlaue=["王翠花"] 可以接收重名的key 比如 name=王翠花&name=刘首富
        if (!queryParams.isEmpty()){
            if (queryParams.containsKey("name")){
                List<String> names = queryParams.get("name");
                //判断name的value是否是空
                if(CollectionUtils.isNotEmpty(names)){
                    System.out.println("query包含name");
                    System.out.println("name的value:"+names);
                }
            }
        }
        //4.请求头
        HttpHeaders headers = request.getHeaders();//包含了所有头对象
        List<String> authorization = headers.get("Authorization");
        if(CollectionUtils.isNotEmpty(authorization)){
            System.out.println("请求头包含Authorization");
            System.out.println("Authorization的value:"+authorization);
        }
        //5.获取请求cookie Cookie={key1=value1;key2=value2}
        MultiValueMap<String, HttpCookie> cookies = request.getCookies();
        if (!cookies.isEmpty()){
            //测试一个自定义cookie叫做token
            if (cookies.containsKey("token")){
                List<HttpCookie> token = cookies.get("token");
                if(CollectionUtils.isNotEmpty(token)){
                    System.out.println("请求头包含token");
                    System.out.println("token的value:"+token);
                }
            }
        }
        return chain.filter(exchange);//让请求正常通过这个过滤器 进入后续流程
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
